// 启动服务模块
const express = require('express');
// 连接数据库模块
const mongoose = require('mongoose');
const cors = require('cors');

const { mock, Random } = require('mockjs');

// 各个接口文件
const authRouter = require('./router/authRouter');
const reservation = require('./router/reservation');
const costRouter = require('./router/costRouter');
const doctorRouter = require('./router/doctorRouter');
const hospitalRouter = require('./router/hospitalRouter');
const home = require('./router/home');
const productRouter = require('./router/productRouter');
const orderRouter = require('./router/orderRouter');
const userRouter = require('./router/userRouter');

// 配置mongoose连接选项
mongoose.set('strictQuery', false);

// 连接数据库
mongoose
    .connect('mongodb://127.0.0.1:27017/QWsystem')
    .then(async () => {
        console.log('✅ 数据库连接成功 ==> MongoDB://127.0.0.1:27017/QWsystem');

        // 检查数据库连接状态
        const dbState = mongoose.connection.readyState;
        console.log('📊 数据库连接状态:', dbState === 1 ? '已连接' : '连接中');

        // 开启服务
        const app = express();

        // 解析数据格式
        app.use(express.json({ limit: '10mb' })); // 解析json数据，设置大小限制
        app.use(express.urlencoded({ extended: true, limit: '10mb' })); // 解析urlencoded数据

        // CORS配置 - 使用更全面的方法
        app.use((req, res, next) => {
            res.header('Access-Control-Allow-Origin', '*');
            res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS');
            res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
            
            // 预检请求处理
            if (req.method === 'OPTIONS') {
                res.sendStatus(200);
            } else {
                next();
            }
        });

        // 配置静态资源文件夹
        app.use(express.static('./static'));

        // 添加请求日志中间件
        app.use((req, res, next) => {
            console.log(`📝 ${new Date().toISOString()} - ${req.method} ${req.url}`);
            next();
        });

        // 引入接口文件
        app.use('/api/auth', authRouter);
        app.use('/api/booking', reservation);
        app.use('/api/cost', costRouter);
        app.use('/api/doctor', doctorRouter);
        app.use('/api/hospital', hospitalRouter);
        app.use('/api/home', home);
        app.use('/api/product', productRouter);
        app.use('/api/order', orderRouter);
        app.use('/api/user', userRouter);

        // 健康检查接口
        app.get('/health', (req, res) => {
            res.json({
                code: 0,
                message: '服务器运行正常',
                data: {
                    timestamp: new Date().toISOString(),
                    database: mongoose.connection.readyState === 1 ? '已连接' : '未连接',
                    uptime: process.uptime()
                }
            });
        });

        // 404处理
        app.use((req, res) => {
            res.status(404).json({
                code: 1,
                message: `接口不存在: ${req.method} ${req.originalUrl}`
            });
        });

        // 全局错误处理
        app.use((error, req, res, next) => {
            console.error('🚨 服务器错误:', error);
            res.status(500).json({
                code: 1,
                message: '服务器内部错误',
                error: process.env.NODE_ENV === 'development' ? error.message : undefined
            });
        });

        // 挂载端口
        const server = app.listen(8088, '127.0.0.1', (error) => {
            if (error) {
                console.error('❌ 服务器启动失败:', error);
                return;
            }
            console.log('🚀 服务器启动成功: http://127.0.0.1:8088');
            console.log('📋 可用接口:');
            console.log('   - POST /api/auth/register  (用户注册)');
            console.log('   - POST /api/auth/login     (用户登录)');
        });

        // 优雅关闭
        process.on('SIGTERM', () => {
            console.log('📴 收到SIGTERM信号，正在关闭服务器...');
            server.close(() => {
                console.log('✅ 服务器已关闭');
                mongoose.connection.close();
            });
        });

        // 调用方法插入假数据（根据需要取消注释）
        // doctor();
    })
    .catch((error) => {
        console.error('❌ 数据库连接失败:', error.message);
        console.log('💡 请确保MongoDB服务正在运行：');
        console.log('   - Windows: net start MongoDB');
        console.log('   - Linux/Mac: sudo systemctl start mongod');
        process.exit(1);
    });


// 医生管理信息
async function doctor() {
    const doctor = require('./model/DoctorData');
    const data = mock({
        'list|30': [
            {
                Doctorname: '秦超',
                'avatar|1': [
                    'http://127.0.0.1:8088/default-avatar.jpg',
                    'http://127.0.0.1:8088/default-avatar2.jpg',
                    'http://127.0.0.1:8088/default-avatar3.png',
                ],
                'Courtyardarea|1': ['东院', '西院', '南院', '北院'],
                'Doctorjob|1': ['主任医师', '医科主任', '主科医生'],
                'Department|1': ['内科-肾内科'],
                Tags: ['糖尿病', '尿毒症', '肾病'],
                Doctorcontent: () => Random.cparagraph(),
                'time|1': [
                    Date.now() - 100000,
                    Date.now() - 900000,
                    Date.now() - 1000000,
                    Date.now() - 5000000,
                    Date.now() - 6000000,
                ],
            },
        ],
    });

    await Promise.all(data.list.map((item) => doctor.create(item)));
    console.log('医生管理信息');
}

